
<?php
/*
 * dub_permissions.php
 * @desc	privileges editor
 * @author	Jamie Wilkinson <jamie@tramchase.com>
 *
*/

if (@txpinterface == 'admin') {
	add_privs('dub_permissions', '1,2');
	register_tab("admin", "dub_permissions", "Permissions");
	register_callback("dub_permissions", "dub_permissions");
}

function dub_permissions($event, $step) {
	global $txp_msg;
	include_once txpath.'/publish.php';
	
    
	$event = gps('event');
	$step = gps('step');
	$handler  = $step;
	if($handler == "dub_permissions" /* default */ || !function_exists($handler)) {
		$handler = "dub_permissions_list";
	}

	msg( msg().' - step: '.$step.' handler:'.$handler);
    pagetop("Permissions Editor", msg() );
	print $handler(); // used to pass $event & $step but they don't really care... should be broken up anyway, no specific handling (?)
}

function dub_permissions_list() {
	global $txp_permissions;
	$r = array();

	$out .= '<table border="1" cellpadding="5" cellspacing="0">';
	foreach($txp_permissions as $r) {
		$priv = key($txp_permissions);
		if(!$priv)
			break;
		$privs_labels = array(1,2,3,4,5,6);
		$lineout = '';
		$lineout .= '<tr><td>';
		$lineout .= $priv;
		$lineout .= '</td>';
		$split_perms = explode(',', $r);
		foreach($privs_labels as $num) {
			$lineout .= '<td>'.$num.': '.dub_permissions_list_input($priv, $num, $split_perms[$num - 1]) . "</td>" ;
		}
		$lineout .= "</tr>\n";
		$out .= $lineout;
	}
	$out .= '</table>';	
	// let them know the action...
	$out .= fInput('submit','','Modify').eInput('dub_permissions').sInput('dub_permissions_save');
	$out = form($out);
	
	msg('listing all perms');
	return $out;
}

function dub_permissions_list_input($priv, $num, $val) {
	$priv = str_replace('.', '-', $priv); // change . to - (safer than letting fInput change it to _, which is used by mad plugins)
	return fInput('checkbox', $priv.'-'.$num, $val, '', '', '', 4 /* size */, '', '', $val);
}

function dub_permissions_save() {
	msg('saving perms!');
	$out = 'PERMS SAVED!';
	print '<pre>';
	print_r($_POST);
	print '</pre>';
	return $out;
}
?>
